﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;


namespace BCNPCObject.EXCEL
{
    public class cls_HDG_DIEM_DO_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private HDG_DIEM_DO info;

        #endregion

        #region   Constructor

        public cls_HDG_DIEM_DO_Controller()
        {
            info = new HDG_DIEM_DO();
        }
        public cls_HDG_DIEM_DO_Controller(HDG_DIEM_DO Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public HDG_DIEM_DO pInfor
        {
            get { return info; }
            set { info = value; }
        }
        #endregion

        #region Methods
        public int ID_DVI_DCHINH(string ma_dviqly)
        {
            int id;
            var query = from p in CMIS2.DB.D_DVI_QLY
                        where (p.MA_DVIQLY == ma_dviqly)
                        select p;
            if (query != null && query.Take(1).Count() > 0)
            {
                id = Convert.ToInt32(query.First().ID_DIA_CHINH);
                return id;
            }
            else
            { return -1; }
        }
        public bool insert_HDG_DIEM_DO(ref string maloi)
        {
            //long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_HDG_HOP_DONG.NEXTVAL from dual");
            //if (_ID == -1) return false;
            try
            {
                //info.HDG_KHACH_HANG = Convert.ToInt64(_ID);
                HDG_DIEM_DO objHDG_DIEM_DO = new HDG_DIEM_DO()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    MA_DDO = info.MA_DDO,
                    ID_HDONG = info.ID_HDONG,
                    MA_KHANG = info.MA_KHANG,
                    MA_DDO_DDIEN = info.MA_DDO_DDIEN,
                    MA_CAPDA = info.MA_CAPDA,
                    DIA_CHI = info.DIA_CHI,
                    ID_DIA_CHINH = info.ID_DIA_CHINH,
                    KIMUA_CSPK = info.KIMUA_CSPK,
                    CSUAT = info.CSUAT,
                    MUC_DICH = info.MUC_DICH,
                    SO_PHA = info.SO_PHA,
                    SOHUU_LUOI = info.SOHUU_LUOI,
                    LOAI_DDO = info.LOAI_DDO,
                    SO_HO = info.SO_HO,
                    LAN_CAPNHAT = info.LAN_CAPNHAT,
                    THAO_TACDL = info.THAO_TACDL,
                    NGAY_HLUC = info.NGAY_HLUC,
                    MO_TA = info.MO_TA,
                    TRANG_THAI = info.TRANG_THAI,
                    TTRANG_TREOTHAO = info.TTRANG_TREOTHAO,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.HDG_DIEM_DO.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                //id_HDG_HOP_DONG = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }
        public bool delete_HDG_DIEM_DO(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        var query = from p in CMIS2.DB.HDG_DIEM_DO
                                    where (p.MA_DVIQLY == dt.Rows[i]["MA_DVIQLY"].ToString() && p.MA_DDO == dt.Rows[i]["MA_DDO"].ToString() && p.NGAY_HLUC == Convert.ToDateTime(dt.Rows[i]["NGAY_HLUC"]) && p.LAN_CAPNHAT == 1)
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.HDG_DIEM_DO.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
